//
//  FPViewController.m
//  Gunz
//
//  Created by Joggy on 16/1/31.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "FPViewController.h"

@implementation FPViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(endEdit)];
    [self.view addGestureRecognizer:tap];
}

-(void)prepareForNav {
    [super prepareForNav];
    self.title = @"找回密码";
    //返回按键
    UIImage *leftImage = [UIImage imageNamed:@"all_back"];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, (leftImage.size.width/leftImage.size.height)*22, 22)];
    [leftButton setImage:leftImage forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self navigationItem].leftBarButtonItem = leftBarItem;
}

-(void)prepareForLayout {
    [super prepareForLayout];
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - BBViewHeight)];
    scroll.contentSize = CGSizeMake(screenWidth, scroll.frame.size.height + 1);
    scroll.delegate = self;
    UIView *accountView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, FPTextHeihgt)];
    //label
    accountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    NSMutableAttributedString *accountTitle = [[NSMutableAttributedString alloc] initWithString:@"账尼玛号 : "];
    NSRange titleRange1 = {0, accountTitle.length};
    NSRange titleRange2 = {1, 2};
    [accountTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font15*proportion] range:titleRange1];
    [accountTitle addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:titleRange2];
    accountLabel.attributedText = accountTitle;
    accountLabel.alpha = 0.7;
    [accountLabel sizeToFit];
    accountLabel.layer.position = CGPointMake(accountLabel.frame.size.width/2, accountView.frame.size.height/2);
    [accountView addSubview:accountLabel];
    //text
    accountText = [[UITextField alloc] initWithFrame:CGRectMake(accountLabel.frame.size.width, 0, FPTextWidth, FPTextHeihgt)];
    accountText.font = [UIFont systemFontOfSize:font14*proportion];
    accountText.placeholder = @"手机号/邮箱";
    accountText.layer.borderColor = TEXT_BORDER_COLOR.CGColor;
    accountText.layer.borderWidth = 1;
    accountText.layer.cornerRadius = 6;
    accountText.backgroundColor = [UIColor whiteColor];
    accountText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FPInsert/4, FPTextHeihgt)];
    accountText.leftViewMode = UITextFieldViewModeAlways;
    [accountView addSubview:accountText];
    //调整accountView
    accountView.frame = CGRectMake(0, 0, accountLabel.frame.size.width + accountText.frame.size.width, FPTextHeihgt);
    accountView.layer.position = CGPointMake(screenWidth/2, accountView.frame.size.height/2 + FPInsert);
    [scroll addSubview:accountView];
    [self.view addSubview:scroll];
    //添加底部按键
    nextButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, screenHeight - BBViewHeight - 64, screenWidth, BBViewHeight) name:@"下一步"];
    [nextButton.but addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
}

-(void)back {
    [self dismissViewControllerAnimated:true completion:nil];
}

-(void)nextStep {
    NSLog(@"忘记密码下一步");
    FPNextViewController *next = [[FPNextViewController alloc] init];
    [next passAccount:@"nima"];
    AFHTTPSessionManager *manager = [GZNetRequest getPOSTManager];
    NSString *findURL = @"http://120.27.138.167/user/forgot";
    NSDictionary *para = @{@"username": @"18351925597"};
    [manager POST:findURL parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%s", "sucess");
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%s", "failure");
        NSLog(@"%@", error);
    }];
    [self.navigationController pushViewController:next animated:true];
}

-(void)endEdit {
    [self.view endEditing:true];
}

-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView {
    [self endEdit];
}

@end
